package inutan.game.condition.task;


import inutan.common.constant.DataConstant;
import inutan.data.Mission.MissionVal;
import inutan.data.Mission.TargetType;
import inutan.game.business.GameFlowContext;
import inutan.game.context.GameUserContext;
import inutan.game.event.model.RoleLevelUpEvent;
import inutan.game.storage.StorageContext;
import inutan.game.task.BaseCondition;
import inutan.game.task.TaskCondition;
import inutan.game.task.TaskEntity;
import luban.common.Command;
import org.ricks.orm.OrmContext;
import java.util.List;
import java.util.function.Consumer;

/**
 * 角色升级条件触发机制
 */
public class RoleLevelUpCondition extends TaskCondition<RoleLevelUpEvent>  {


    @Override
    public void emit(RoleLevelUpEvent roleLevelUpEvent) {
        System.err.println("角色升级触发条件。。。。。");
        GameFlowContext context = roleLevelUpEvent.getContext();
        Consumer<TaskEntity> consumer = taskEntity -> {
            taskEntity.setProgress(roleLevelUpEvent.getLv());
            OrmContext.me().update(taskEntity);
            if(taskEntity.getMissionVal().targetCfg1 >= taskEntity.getProgress()) {
                //任务完成直接发任务奖励 并且 推送
                context.sendResponse(DataConstant.BROADCAST_MESSAGE, Command.CompleteTasks); //推送任务完成

            }
        };


        //1.查询到玩家所有的角色升级任务
        List<TaskEntity> roleLevelUpTask = OrmContext.me().multiCacheLoadAll(TaskEntity.class, context.getUserId(), task -> {
            MissionVal missionVal = StorageContext.tables().getTbMission().get(task.getTaskId());
            task.setMissionVal(missionVal); //load db
            return missionVal.targetType == TargetType.MissionTargetType015;
        });
        roleLevelUpTask.forEach(consumer);
    }

    @Override
    public boolean isDone(RoleLevelUpEvent roleLevelUpEvent) {
        return false;
    }



}
